package com.tx.admin.detailmodepicture.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.tx.admin.detailmodepicture.service.DetailModePictureService;
import com.tx.admin.detailmodepicture.pojo.po.DetailModePicture;
import com.tx.admin.detailmodepicture.mapper.DetailModePictureMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.tx.core.common.enums.CommonResponseEnum;
import com.tx.core.util.PageUtils;
import com.tx.core.util.BeanCopyUtils;
import com.tx.core.util.Utils;
import com.tx.core.common.entity.PageResult;
import com.tx.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.tx.admin.detailmodepicture.pojo.dto.DetailModePictureCreateDTO;
import com.tx.admin.detailmodepicture.pojo.dto.DetailModePictureUpdateDTO;
import com.tx.admin.detailmodepicture.pojo.dto.DetailModePictureListDTO;
import com.tx.admin.detailmodepicture.pojo.dto.DetailModePictureImportDTO;
import com.tx.core.common.entity.ImportExcelDTO;
import com.tx.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.tx.core.util.FileUtils;
import com.tx.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.tx.admin.detailmodepicture.pojo.vo.DetailModePictureVO;

/**
 * <p>
 * 详情页模版图片 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-08-05
 */
@Service
@RequiredArgsConstructor
public class DetailModePictureServiceImpl extends ServiceImpl<DetailModePictureMapper, DetailModePicture> implements DetailModePictureService {
    @Override
    public void create(DetailModePictureCreateDTO dto){
        DetailModePicture detailModePicture = BeanCopyUtils.copy(dto, DetailModePicture.class);
        if ("1".equals(dto.getIsAvatar())){
            QueryWrapper wrapper = QueryWrapper.create().from(DetailModePicture.class);
            remove(wrapper.eq(DetailModePicture::getBelongId,dto.getBelongId()).eq(DetailModePicture::getIsAvatar,1));
        }

        save(detailModePicture);
    }

    @Override
    public void update(DetailModePictureUpdateDTO dto){
        DetailModePicture detailModePicture = BeanCopyUtils.copy(dto, DetailModePicture.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(DetailModePicture::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(detailModePicture);
    }

    @Override
    public PageResult<DetailModePictureVO> page(DetailModePictureListDTO dto){
        Page<DetailModePictureVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), DetailModePictureVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<DetailModePictureVO> list(DetailModePictureListDTO dto){
        return listAs(buildQueryWrapper(dto), DetailModePictureVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public DetailModePictureVO detail(Object id){
        DetailModePicture detailModePicture = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(detailModePicture);
        return BeanCopyUtils.copy(detailModePicture, DetailModePictureVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<DetailModePictureImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), DetailModePictureImportDTO.class, true);
        List<DetailModePictureImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(DetailModePictureListDTO dto, HttpServletResponse response) {
        List<DetailModePictureVO> list = list(dto);
        String fileName = "详情页模版图片模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "详情页模版图片", DetailModePictureVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(DetailModePictureListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(DetailModePicture.class);
        if (Utils.isNotNull(dto.getBelongId())) {
            wrapper.eq(DetailModePicture::getBelongId, dto.getBelongId());
        }
        if (Utils.isNotNull(dto.getPictureId())) {
            wrapper.eq(DetailModePicture::getPictureId, dto.getPictureId());
        }
        if (Utils.isNotNull(dto.getIsAvatar())) {
            wrapper.eq(DetailModePicture::getIsAvatar, dto.getIsAvatar());
        }
        if (Utils.isNotNull(dto.getDes())) {
            wrapper.eq(DetailModePicture::getDes, dto.getDes());
        }
        if (Utils.isNotNull(dto.getExt())) {
            wrapper.eq(DetailModePicture::getExt, dto.getExt());
        }
        if (Utils.isNotNull(dto.getExt1())) {
            wrapper.eq(DetailModePicture::getExt1, dto.getExt1());
        }
        if (Utils.isNotNull(dto.getExt2())) {
            wrapper.eq(DetailModePicture::getExt2, dto.getExt2());
        }
        return wrapper;
    }
}